#include "a9main.h"

int cgiMain()
{
	key_t  key;
	char buf[20];
    status new;
	int msgid;
	CGI_cmd msg_buf;
	
	memset(&msg_buf,0,sizeof(msg_buf));

	cgiFormString("temMAX", buf, 20);
	new.tem_max = atoi (buf);
	cgiFormString("temMIN", buf, 20);
	new.tem_min = atoi (buf);
	cgiFormString("illMAX", buf, 20);
	new.light_max = atoi (buf);
	cgiFormString("illMIN", buf, 20);
	new.light_min = atoi (buf);
	cgiFormString("humMAX", buf, 20);
	new.hum_max = atoi (buf);
	cgiFormString("humMIN", buf, 20);
	new.hum_min = atoi (buf);

	key = ftok("/app",'g');
	msgid =msgget(key,IPC_CREAT|IPC_EXCL|0666);
	if(msgid == -1)
	{
		if(errno == EEXIST)
		{
			msgid = msgget(key,0666);
		}else{
			perror("fail to magget");
			exit(1);
		}
	}
	
	
	msg_buf.mytype = 1;
	msg_buf.type = set_ctl;
	
	memcpy (&msg_buf.text[0], &new, sizeof(new));
	msgsnd(msgid,(void *)&msg_buf,sizeof(msg_buf)-sizeof(long),0);

	cgiHeaderContentType("text/html\n\n"); 
	fprintf(cgiOut, "<HTML><HEAD>\n"); 
	fprintf(cgiOut, "<TITLE>My CGI</TITLE></HEAD>\n"); 
	fprintf(cgiOut, "<BODY>"); 

	fprintf(cgiOut, "<H2>send sucess</H2>");

	//fprintf(cgiOut, "<a href='.html'>返回</a>"); 
	fprintf(cgiOut, "<meta http-equiv=\"refresh\" content=\"1;url=../main1.html\">");
	fprintf(cgiOut, "</BODY>\n"); 
	fprintf(cgiOut, "</HTML>\n"); 


	return 0; 

}
